<?php

/*
 * Парсим картинки для OpenCart.
 * 
 * eosagm@gmail.com
 * 
 * icq# 217861501
 * 
 * 
 */

class ControllerModuleParseImage extends Controller {

    public function __construct($registry) {
        parent::__construct($registry);

        $this->load->model('tool/parseimage');
    }

    public function index() {

        $this->load->language('module/parseimage');
        $this->document->setTitle($this->language->get('heading_title'));
        $this->data['heading_title'] = $this->language->get('heading_title');
        $this->data['button_next'] = $this->language->get('button_next');
        $this->data['button_search'] = $this->language->get('button_search');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_module'),
            'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('module/parseimage', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['action'] = $this->url->link('module/parseimage', 'token=' . $this->session->data['token'], 'SSL');

        $this->data['searchtext'] = '';


        //-----------------------------------------------------------------------------------------------------------------


        $this->data['token'] = $this->session->data['token'];
        $this->data['product'] = array();

        if (empty($_POST) && empty($_GET['id'])) {

            $this->data['product'] = $this->model_tool_parseimage->getProduct();
        }


        if (!empty($_GET['id'])) {
            $this->data['product'] = $this->model_tool_parseimage->getProduct((int) $_GET['id']);
        }


        if (isset($this->request->post['searchtext'])) {

            $this->data['product'] = $this->model_tool_parseimage->getProduct((int) $this->request->post['id'], $this->request->post['searchtext']);
            $this->data['searchtext'] = $this->request->post['searchtext'];
        }


        $this->template = 'module/parseimage.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );
        $this->response->setOutput($this->render(), $this->config->get('config_compression'));
    }

    public function getimage() {

        if (!empty($this->request->request['getimage'])) {

            $id = $this->request->request['id'];
            $image_name = $this->request->request['getimage'];

            $this->model_tool_parseimage->insertImage($image_name, $id);

            echo "Изображение " . $image_name . " к id=" . $id . " добавлено.";
            
        }
    }

    protected function validate() {
        if (!$this->user->hasPermission('modify', 'module/account')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

}

?>
